﻿; *****************************************************************************
; Inno Setup Script for Transmission Remote: Magnet Friendly Edition
; https://code.google.com/p/transmission-remote-dotnet-magnet-friendly-edition/
; Copyright (c) 2014  __alex
; Licensed under GNU General Public License v3
; *****************************************************************************

; .net Framework 4.0 Full is required for this program
#define use_dotnetfx40

#define MyAppSetupName 'Transmission Remote - Magnet Friendly Edition'
#define MyAppVersion '2'

; for PinToTaskbar function
#define ExeName "Transmission Remote.exe"    ; Exe-файл, который нужно закрепить 

[Setup]
AppName=Transmission Remote: Magnet Friendly Edition
AppVersion={#MyAppVersion}
AppVerName=Transmission Remote: Magnet Friendly Edition {#MyAppVersion}
AppPublisher="Alan F., __alex"
AppPublisherURL=https://code.google.com/p/transmission-remote-dotnet-magnet-friendly-edition/
AppMutex={#MyAppSetupName}
DefaultDirName={pf}\{#MyAppSetupName}
DefaultGroupName={#MyAppSetupName}
Compression=lzma2
SolidCompression=yes
SetupIconFile=images\newlogo_black_install-8bit.ico
WizardImageFile=images\newlogo_white_install.bmp
WizardSmallImageFile=images\newlogo_black_install-8bit.bmp
UninstallDisplayIcon={app}\Transmission Remote.exe
LicenseFile=..\LICENCE.txt
OutputDir=.
OutputBaseFilename={#MyAppSetupName} {#MyAppVersion} Setup
ArchitecturesInstallIn64BitMode=x64
ChangesAssociations=yes

[Languages]
Name: en; MessagesFile: compiler:Default.isl
Name: ru; MessagesFile: compiler:Languages\Russian.isl

[CustomMessages]
en.RegisterAsDefaultApp=Register as a default application for
en.TorrentFiles=.torrent files
en.MagnetLinks=Magnet links
en.DeleteAppSettings=Do you want to delete program settings?

ru.RegisterAsDefaultApp=Зарегистрировать как приложение по-умолчанию для
ru.TorrentFiles=.torrent файлов
ru.MagnetLinks=Магнитных ссылок
ru.DeleteAppSettings=Удалить настройки программы?

[Tasks]
; icons
Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}
Name: quicklaunchicon; Description: {cm:CreateQuickLaunchIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked; OnlyBelowVersion: 6.1
Name: PinToTaskbar; Description: {cm:cTaskbarTxt}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked; MinVersion: 6.1
; filetype associations
Name: AssociateWithTorrentFiles; Description: {cm:TorrentFiles}; GroupDescription: {cm:RegisterAsDefaultApp}
Name: AssociateWithMagnetLinks; Description: {cm:MagnetLinks}; GroupDescription: {cm:RegisterAsDefaultApp}

[Files]
; Source: Input\*; DestDir: {app}; Flags: recursesubdirs

Source: "..\GeoIP.dat"; DestDir: {app}
Source: "..\TransmissionClientNew\Jayrock\Release\Jayrock.dll"; DestDir: {app}
Source: "..\TransmissionClientNew\Jayrock\Release\Jayrock.Json.dll"; DestDir: {app}
Source: "..\LICENCE.txt"; DestDir: {app}
Source: "..\TransmissionClientNew\bin\Release\Raccoom.Xml.dll"; DestDir: {app}
Source: "..\README-Magnet Friendly Edition.txt"; DestDir: {app}
Source: "..\README-original.txt"; DestDir: {app}
Source: "..\TransmissionClientNew\bin\Release\Transmission Remote.exe"; DestDir: {app}
Source: "..\TransmissionClientNew\bin\Release\cs-CZ\*"; DestDir: {app}\cs-CZ
Source: "..\TransmissionClientNew\bin\Release\da-DK\*"; DestDir: {app}\da-DK
Source: "..\TransmissionClientNew\bin\Release\de-DE\*"; DestDir: {app}\de-DE
Source: "..\TransmissionClientNew\bin\Release\es-ES\*"; DestDir: {app}\es-ES
Source: "..\TransmissionClientNew\bin\Release\fr-FR\*"; DestDir: {app}\fr-FR
Source: "..\TransmissionClientNew\bin\Release\hu-HU\*"; DestDir: {app}\hu-HU
Source: "..\TransmissionClientNew\bin\Release\ko-KR\*"; DestDir: {app}\ko-KR
Source: "..\TransmissionClientNew\bin\Release\nl-NL\*"; DestDir: {app}\nl-NL
Source: "..\TransmissionClientNew\bin\Release\pl-PL\*"; DestDir: {app}\pl-PL
Source: "..\TransmissionClientNew\bin\Release\pt-BR\*"; DestDir: {app}\pt-BR
Source: "..\TransmissionClientNew\bin\Release\ru-RU\*"; DestDir: {app}\ru-RU
Source: "..\TransmissionClientNew\bin\Release\sv-SE\*"; DestDir: {app}\sv-SE
Source: "..\TransmissionClientNew\bin\Release\tr-TR\*"; DestDir: {app}\tr-TR
Source: "..\TransmissionClientNew\bin\Release\zh-CN\*"; DestDir: {app}\zh-CN
Source: "..\TransmissionClientNew\bin\Release\zh-TW\*"; DestDir: {app}\zh-TW

[Registry]
; do not create, but instead mark registry key for deletion at the Uninstall step.
; This registry key is not used by the current installer or the app itself. 
; It's just remains of the original Transmission Remote.
Root: HKLM; Subkey: "Software\TransmissionRemote"; Flags: dontcreatekey uninsdeletekey

; AssociateWithTorrentFiles
Root: HKCR; Subkey: ".torrent"; ValueType: string; ValueName: ""; ValueData: "Transmission Remote Torrent"; Flags: uninsdeletevalue; Tasks: AssociateWithTorrentFiles
Root: HKCR; Subkey: "Transmission Remote Torrent"; ValueType: string; ValueName: ""; ValueData: "Transmission Remote Torrent"; Flags: uninsdeletekey; Tasks: AssociateWithTorrentFiles
Root: HKCR; Subkey: "Transmission Remote Torrent\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\Transmission Remote.exe,0"; Tasks: AssociateWithTorrentFiles
Root: HKCR; Subkey: "Transmission Remote Torrent\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\Transmission Remote.exe"" ""%1"""; Tasks: AssociateWithTorrentFiles
; AssociateWithMagnetLinks
Root: HKCR; Subkey: "magnet"; ValueType: string; ValueName: ""; ValueData: "Magnet URI"; Flags: uninsdeletekey; Tasks: AssociateWithMagnetLinks
Root: HKCR; Subkey: "magnet"; ValueType: string; ValueName: "URL Protocol"; ValueData: ""; Tasks: AssociateWithMagnetLinks
Root: HKCR; Subkey: "magnet\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\Transmission Remote.exe,0"; Tasks: AssociateWithMagnetLinks
Root: HKCR; Subkey: "magnet\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\Transmission Remote.exe"" ""%1"""; Tasks: AssociateWithMagnetLinks

[Icons]
Name: {group}\{#MyAppSetupName}; Filename: {app}\Transmission Remote.exe
Name: {group}\{cm:UninstallProgram,{#MyAppSetupName}}; Filename: {uninstallexe}
Name: {commondesktop}\{#MyAppSetupName}; Filename: {app}\Transmission Remote.exe; Tasks: desktopicon
Name: {userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppSetupName}; Filename: {app}\Transmission Remote.exe; Tasks: quicklaunchicon

[Run]
Filename: {app}\Transmission Remote.exe; Description: {cm:LaunchProgram,Transmission Remote: Magnet Friendly Edition {#MyAppVersion}}; Flags: nowait postinstall skipifsilent

[UninstallDelete]
;Type: dirifempty; Name: {userappdata}\{#MyAppSetupName}

;todo
; remove remains of the original Transmission Remote
; Registry key to check for directory. It's not used in the current installer.
;DeleteRegKey HKLM SOFTWARE\TransmissionRemote

; App settings. todo: checkbox "Remove program settings", checked by default
;DeleteRegKey HKCU "Software\TransmissionRemote"

;******************************************************************************

#include "dependencies\scripts\products.iss"

#include "dependencies\scripts\products\stringversion.iss"
#include "dependencies\scripts\products\winversion.iss"
#include "dependencies\scripts\products\fileversion.iss"
#include "dependencies\scripts\products\dotnetfxversion.iss"

#ifdef use_dotnetfx40
#include "dependencies\scripts\products\dotnetfx40full.iss"
#endif


[CustomMessages]
ru.cTaskbarTxt=Закрепить на панели задач
ru.cStartMenuTxt=Закрепить в меню "Пуск" / на начальном экране
en.cTaskbarTxt=Pin to Taskbar
en.cStartMenuTxt=Pin to Start / Start Menu

[Code]
const
    LOAD_LIBRARY_AS_DATAFILE = $2;
 
#define A = (Defined UNICODE) ? "W" : "A"
 
function LoadLibraryEx(lpFileName: String; hFile: THandle; dwFlags: DWORD): THandle; external 'LoadLibraryEx{#A}@kernel32.dll stdcall';
function LoadString(hInstance: THandle; uID: SmallInt; var lpBuffer: Char; nBufferMax: Integer): Integer; external 'LoadString{#A}@user32.dll stdcall';
function SHGetNewLinkInfo(pszLinkTo, pszDir: String; var pszName: Char; var pfMustCopy: Longint; uFlags: UINT): BOOL; external 'SHGetNewLinkInfo{#A}@shell32.dll stdcall';
 
function PinToTaskbar(const szFilename: String; TaskbarOrStart, IsPin: Boolean): Boolean;
//szFilename: full path to executable file
//TaskbarOrStart: True - pin to Taskbar, False - pin to Start Menu
//IsPin: True - pin to Taskbar / Start Menu, False - unpin
var
    hInst: THandle;
    buf: array [0..255] of Char;
    i, res: Integer;
    strLnk, strVerb: String;
    objShell, colVerbs: Variant;
begin
    Result := False;
    if (GetWindowsVersion < $06010000) or not FileExists(szFilename) then Exit;
    if IsPin then
    begin
        if SHGetNewLinkInfo(szFilename, ExpandConstant('{tmp}'), buf[0], res, 0) then
        begin
            while buf[Length(strLnk)] <> #0 do Insert(buf[Length(strLnk)], strLnk, Length(strLnk)+1);
            if FileExists(ExpandConstant('{userappdata}\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\') + ExtractFileName(strLnk)) then Exit;
        end;
        if TaskbarOrStart then res := 5386 else res := 5381;
    end else if TaskbarOrStart then res := 5387 else res := 5382;
    hInst := LoadLibraryEx(ExpandConstant('{sys}\shell32.dll'), 0, LOAD_LIBRARY_AS_DATAFILE);
    if hInst <> 0 then
    try
        for i := 0 to LoadString(hInst, res, buf[0], 255)-1 do Insert(buf[i], strVerb, i+1);
        try
            objShell := CreateOleObject('Shell.Application');
            colVerbs := objShell.Namespace(ExtractFileDir(szFilename)).ParseName(ExtractFileName(szFilename)).Verbs;
            for i := 1 to colVerbs.Count do if CompareText(colVerbs.Item[i].Name, strVerb) = 0 then
            begin
                colVerbs.Item[i].DoIt;
                Result := True;
                Break;
            end;
        except
            Exit;
        end;
    finally
        FreeDLL(hInst);
    end;
end;
 
procedure CurPageChanged(CurPageID: integer);
begin
  case CurPageID of
    wpFinished: begin
      if WizardForm.TasksList.Checked[2] then
        PinToTaskbar(WizardDirValue + '\{#ExeName}', True, True);
//       if WizardForm.TasksList.Checked[1] then
//         PinToTaskbar(WizardDirValue + '\{#ExeName}', False, True);
    end;
  end;
end;
 
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  instPath: string;
begin
  // unpin from taskbar and start menu
  if RegQueryStringValue(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppName")}_is1',
    'InstallLocation', instPath) then begin
    case CurUninstallStep of
      usUninstall: begin
        PinToTaskbar(instPath + '{#ExeName}', True, False);
        PinToTaskbar(instPath + '{#ExeName}', False, False);
      end;
    end;
  end;
  
  // delete app settings
  if CurUninstallStep = usPostUninstall then
  begin
    if MsgBox(CustomMessage('DeleteAppSettings'),
      mbConfirmation, MB_YESNO) = IDYES
    then
      RegDeleteKeyIncludingSubkeys(HKEY_CURRENT_USER, 'Software\TransmissionRemote');
  end;  
end; 

[CustomMessages]
win_sp_title=Windows %1 Service Pack %2

[Code]
function InitializeSetup(): boolean;
var
  ResultCode: Integer;
begin
	//init windows version
	initwinversion();

// .netfx 4.0 FULL is required!
#ifdef use_dotnetfx40
  if (not netfxinstalled(NetFx40Full, '')) then
  dotnetfx40full();
#endif

//   PinToTaskbar('C:\Windows\System32\mspaint.exe', true, true);
//   MsgBox('pinned! press ok to unpin', mbInformation, MB_OK);
//   PinToTaskbar('C:\Windows\System32\mspaint.exe', true, false);
	Result := true;
end;


